# -*- coding: utf-8 -*-

"""
DateTime   : 2021/04/06 10:55
Author     : ZhangYafei
Description: 
"""
import os

import requests


image_type_info = {
        0: '最新壁纸', 6: '美女模特', 30: '爱情美图', 9: '风景大片',
        36: '4K专区', 15: '小清新', 26: '动漫卡通', 11: '明星风尚',
        14: '萌宠动物', 5: '游戏壁纸', 12: '汽车天下', 10: '炫酷时尚',
        29: '月历壁纸', 7: '景视剧照', 35: '文字控', 13: '节日美图',
        22: '军事天地', 16: '劲爆体育', 18: 'BABY秀'
}

def main(start: int, count: int, image_type: int):
    url = 'https://bird.ioliu.cn/v2'

    if image_type == 0:
        params = {'url': f'http://wallpaper.apc.360.cn/index.php?c=WallPaper&start={start}&count={count}&from=360chrome&a=getAppsByOrder&order=create_time'}
    else:
        params = {'url': f'http://wallpaper.apc.360.cn/index.php?c=WallPaper&start={start}&count={count}&from=360chrome&a=getAppsByCategory&cid={image_type}'}

    # 图片文件夹分类
    image_type_desc = image_type_info[image_type]
    if not os.path.exists(f'images/{image_type_desc}'):
        os.makedirs(f'images/{image_type_desc}')

    # 发送请求
    response = requests.get(url=url, params=params)
    result = response.json()

    for image_info in result['data']:
        uid = image_info['id']
        image_url = image_info['url']
        tags = image_info['tag'].replace('_category_', '').replace('_全部_', '').strip('_ ').replace(' ', '')
        size = image_info['resolution'].replace('x', '_')
        filepath = f'images/{image_type_info[image_type]}/{tags}_{uid}_{size}.jpg'
        response = requests.get(image_url)
        with open(filepath, mode='wb') as f:
            f.write(response.content)
        print(f'{image_url} {filepath} 下载成功')



if __name__ == '__main__':
    main(start=1, count=12, image_type=0)